#pragma once

#include <stdlib.h>
#include <string.h>

#include "Tools.h"

#define PARAM_LIST_SIZE 3

typedef struct Command {
    char *commandStr;
    char *parameter[PARAM_LIST_SIZE];
    int pSize;
} Command;

static Command *Command_create(char *buffer) {
    Command *command = (Command *) malloc(sizeof(Command));
    command->pSize = 0;
    char *startPtr = buffer;
    int i = 0;
    while (i < MAX_INPUT_SIZE && buffer[i] == ' ') {
        i++;
        startPtr++;
    }
    command->commandStr = startPtr;
    for (; i < MAX_INPUT_SIZE; i++) {
        if (buffer[i] == '\n') {
            buffer[i] = '\0';
            break;
        }
        if (buffer[i] != ' ') {
            continue;
        }
        if (startPtr == buffer + i) {
            startPtr++;
            continue;
        }
        buffer[i] = '\0';
        if (command->commandStr != startPtr) {
            RETURN_IF_TRUE(command->pSize >= PARAM_LIST_SIZE, command)
            command->parameter[command->pSize++] = startPtr;
        }
        startPtr = buffer + i + 1;
    }
    if (command->commandStr != startPtr && startPtr != buffer + i) {
        RETURN_IF_TRUE(command->pSize >= PARAM_LIST_SIZE, command)
        command->parameter[command->pSize++] = startPtr;
    }
    RETURN_IF_TRUE(command->commandStr[0] == '\0', NULL)
    return command;
}

static void Command_print(Command *command) {
    DEBUG_BEGIN("print command")
        printf("%s", command->commandStr);
        puts("-----");
        for (int i = 0; i < command->pSize; i++) {
            for (int j = 0; j < strlen(command->parameter[i]); j++) {
                putchar(command->parameter[i][j]);
            }
            puts("-------");
        }
    DEBUG_END
}